package com.maochang.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.maochang.pojo.PsLineInfo;
import com.maochang.pojo.PsPoleInfo;
import com.maochang.pojo.PsSystemconfig;
import com.maochang.pojo.PsUser;
import com.maochang.pojo.util.Result;
import com.maochang.pojo.util.Result1;
import com.maochang.pojo.util.Result2;
import com.maochang.service.PsLineInfoService;
import com.maochang.service.PsPoleInfoService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 线路管理
 * */

@Slf4j
@RestController
@RequestMapping("/XianLuGuanLi")
public class XianLuGuanLi {

    @Resource
    private PsLineInfoService psLineInfoService;
    @Resource
    private PsPoleInfoService psPoleInfoService;


    private String msg;

    @GetMapping("/XianLu")
    public ModelAndView index(Model model){
        return new ModelAndView("xianlu/XianLuGuanLi");
    }

    @GetMapping("/XianLuCha")
    public Result1 ChaXianLu(String lineName,Integer id,@RequestParam("page") int current,@RequestParam("limit") int size){
        Page<PsLineInfo> aa= psLineInfoService.ChaPsLI(lineName,id,current,size);
        Result1 list=Result1.success1(aa);
        return list;
    }

    @PostMapping("/qxZhuAngTaiGai")
    public  Result qxZhuAngTaiGai(Integer id,Integer zhuAngTai){
        Result result =new Result();
        boolean fAlg=psLineInfoService.QxZhuAngTai(id,zhuAngTai);
        if(fAlg==true){
            result.setMsg("状态更新成功");
        }
        return result;
    }

    @PostMapping("/add")
    @SneakyThrows
    public Result add(PsLineInfo psIn, HttpSession session){
        System.out.println(psIn.toString()+"这是啥");
        PsUser s= (PsUser) session.getAttribute("loginUser");
        psIn.setCreatedBy(s.getUserName());
            psLineInfoService.addPsIF(psIn);
            return Result.success("添加用户成功");
    }

    @PostMapping("/del")
    public  Result ShanCHu(Integer id){
        Result result =new Result();
        boolean fAlg=psLineInfoService.ShanChu(id);
        if(fAlg){
            result.setMsg("成功");
            return result;
        }else {
            result.setMsg("失败");
            return result;
        }
    }

    @PostMapping("/PsLIUpdate")
    public  Result qxTypeUpdate(PsLineInfo psLineInfo){
        Result result =new Result();
        boolean fAlg=psLineInfoService.PSUpdate(psLineInfo);
        if(fAlg==true){
            result.setMsg("修改成功");
        }
        return result;
    }

    @GetMapping("/XiaLaKuang")
    public List<PsSystemconfig> ChaXia(){
       return psLineInfoService.ChaXia();
    }

    @GetMapping("/ChaXunMeiXianGan")
    public Result ChaXunMeiXianGan(){
      List<PsPoleInfo> s= psLineInfoService.ChaXunMeiXianGan();
        Result list=Result.success(s);
        return list;
    }

    @PostMapping("/ChaZuiDa")
    public List<Result2> ChaZuiDa(@RequestBody List<PsPoleInfo> a){
        System.out.println(a+"lll");
        return psLineInfoService.ChaZuiDa(a);

    }

    @PostMapping("/duoXiuGai")
    public Result XiuGai(@RequestBody Integer[] a,String lineCode,String lineName){

        Result result =new Result();
        for(int i=0;i<a.length;i++){
            System.out.println(a[i]);
        }
        boolean fAlg=psLineInfoService.PsDuoPsole(a,lineCode,lineName);
        if(fAlg==true){
            result.setMsg("修改成功");
        }
        return result;

    }

}
